package com.example.city_home_app.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_News;
import com.example.city_home_app.Bean.Down_NewsComment;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_News;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_NewsComment;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BottomDialog;

public class NewsCommentActivity extends BaseActivity {
    @BindView(R.id.img_new_comment_order)
    ImageView imgNewCommentOrder;
    @BindView(R.id.tv_new_comment_allcomment)
    TextView tvNewCommentAllcomment;
    @BindView(R.id.img_new_comment_close)
    ImageView imgNewCommentClose;
    @BindView(R.id.tv_news_comment_allcomment2)
    TextView tvNewsCommentAllcomment2;
    @BindView(R.id.rec_news_comment)
    RecyclerView recNewsComment;
    @BindView(R.id.img_new_comment_add)
    ImageView imgNewCommentAdd;

    private int newsId;
    private ApiModel model = new ApiModel();
    private Adapter_NewsComment adapter_newsComment;
    private EditText mEditText;
    private boolean isfirst =false;

    private RetrofitListener<List<Down_NewsComment>> retrofitListener = new RetrofitListener<List<Down_NewsComment>>() {
        @Override
        public void onResponse(List<Down_NewsComment> listener) {
            if(adapter_newsComment.getItemCount() != 0) {
               adapter_newsComment.clearList();
            }
            if(listener.size()<=1){
                imgNewCommentOrder.setVisibility(View.INVISIBLE);
            }
            if(listener.size()==0){
                isfirst = true;
                tvNewCommentAllcomment.setText("暂无评论，赶紧评论一下吧!");
                recNewsComment.setVisibility(View.GONE);
                return;
            }
            adapter_newsComment.setList(listener);
            if(isfirst) {
                recNewsComment.setVisibility(View.VISIBLE);
                adapter_newsComment.notifyItemInserted(0);
                isfirst = false;
            }
            adapter_newsComment.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_News> newsRetrofitListener = new RetrofitListener<Down_News>() {
        @Override
        public void onResponse(Down_News listener) {
            tvNewCommentAllcomment.setText("全部"+listener.getNews_comment()+"条评论");
            tvNewsCommentAllcomment2.setText("评论("+listener.getNews_comment()+")");
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> upcommentretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(NewsCommentActivity.this,"评论成功",UIUtils.LENGTH_SHORT);
                model.getnews(newsId,newsRetrofitListener);
                adapter_newsComment.clearList();
                model.getNewsComment(newsId,retrofitListener);
            }else{
                UIUtils.showToast(NewsCommentActivity.this,"服务器出现点问题",UIUtils.LENGTH_SHORT);
            }
        }

        @Override
        public void onFail(String msg) {
            UIUtils.showToast(NewsCommentActivity.this,"服务器出现点问题",UIUtils.LENGTH_SHORT);
        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_news_comment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        recNewsComment = (RecyclerView)findViewById(R.id.rec_news_comment);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recNewsComment.setLayoutManager(layoutManager);
        adapter_newsComment = new Adapter_NewsComment(this,getSupportFragmentManager());
        recNewsComment.setAdapter(adapter_newsComment);
        recNewsComment.setItemAnimator(new DefaultItemAnimator());
    }



    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        newsId = intent.getIntExtra("id",0);
        adapter_newsComment.setNewsid(newsId);
        //model.getnews(newsId,newsRetrofitListener);
        //model.getNewsComment(newsId,retrofitListener);
    }

    BottomDialog dialog;

    @OnClick({R.id.img_new_comment_order, R.id.img_new_comment_close,R.id.img_new_comment_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_new_comment_order:
                adapter_newsComment.reverseList();
                break;
            case R.id.img_new_comment_close:
                finish();
                break;
            case R.id.img_new_comment_add:
                dialog = BottomDialog.create(getSupportFragmentManager());
                        dialog.setViewListener(new BottomDialog.ViewListener() {
                            @Override
                            public void bindView(final View v) {
                                mEditText = (EditText)v.findViewById(R.id.edittext_dialog_newscomment_content);
                                Button button = (Button)v.findViewById(R.id.btn_dialog_newscomment_send);
                                mEditText.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        InputMethodManager imm =
                                                (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                                        imm.showSoftInput(mEditText, 0);
                                    }
                                });
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if(!mEditText.getText().toString().equals("")){
                                            dialog.dismiss();
                                            model.upnewcomment(newsId,BaseActivity.getUserinfo().getUserid(),mEditText.getText().toString(),upcommentretrofitListener);
                                        }else{
                                            UIUtils.showToast(NewsCommentActivity.this,"格式非法",UIUtils.LENGTH_SHORT);
                                        }
                                    }
                                });
                            }
                        })
                        .setLayoutRes(R.layout.newscomment_dialog)
                        .setDimAmount(0.1f)
                        .setCancelOutside(true)
                        .setTag("评论")
                        .show();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        model.getnews(newsId,newsRetrofitListener);
        model.getNewsComment(newsId,retrofitListener);
    }
}
